#include "runshopbean.h"
#include "QJsonDocument"
#include "QJsonArray"
#include "utils/citymanager.h"
#include "setting.h"
#include "utils/runshopgoodsmanager.h"


RunShopBean::RunShopBean()
{

}

RunShopBean::RunShopBean(int num, QString groupCity, QString groupXiaoWeiLocation, int groupIndex,QString remoteCity,QString remoteXiaoWeiLocation,
                         int remoteCityIndex,QString remoteBuyGoods, QString time){
    this->num = num;
    this->groupCity = groupCity;
    this->groupXiaoWeiLocation = groupXiaoWeiLocation;
    this->groupIndex = groupIndex;
    this->remoteCity = remoteCity;
    this->remoteXiaoWeiLocation = remoteXiaoWeiLocation;
    this->remoteCityIndex = remoteCityIndex;
    this->remoteBuyGoods = remoteBuyGoods;
    this->time = time;
}

QList<RunShopBean> RunShopBean::jsonToArray(QByteArray jsonArrayStr)
{
    QJsonParseError jsonError;
    QList<RunShopBean> roleBeans;
    const QJsonDocument &document = QJsonDocument::fromJson(jsonArrayStr, &jsonError);
    if(document.isNull() || jsonError.error != QJsonParseError::NoError) return roleBeans;
    QJsonArray jsonArray = document.array(); //文档转化为Json对象
    for(int i = 0 ; i < jsonArray.size() ; i++){
        QJsonObject obj = jsonArray.at(i).toObject();

        int num = obj.value("num").toInt();
        QString groupCity = obj.value("groupCity").toString();
        QString groupXiaoWeiLocation = obj.value("groupXiaoWeiLocation").toString("");

        int groupIndex = obj.value("groupIndex").toInt();
        QString remoteCity = obj.value("remoteCity").toString();
        QString remoteXiaoWeiLocatoin = obj.value("remoteXiaoWeiLocation").toString("");
        int remoteCityIndex = obj.value("remoteCityIndex").toInt();
        QString remoteBuyGoods = obj.value("remoteBuyGoods").toString();
        QString time = obj.value("time").toString();
        roleBeans.append(RunShopBean(num, groupCity, groupXiaoWeiLocation, groupIndex, remoteCity, remoteXiaoWeiLocatoin, remoteCityIndex, remoteBuyGoods, time));
    }
    return roleBeans;
}

void RunShopBean::createObject(QJsonObject &obj, RunShopBean *runShopBean)
{
    obj.insert("num", runShopBean->num);
    obj.insert("groupCity", runShopBean->groupCity);
    obj.insert("groupXiaoWeiLocation", runShopBean->groupXiaoWeiLocation);
    obj.insert("groupIndex", runShopBean->groupIndex);
    obj.insert("remoteCity", runShopBean->remoteCity);
    obj.insert("remoteXiaoWeiLocation", runShopBean->remoteXiaoWeiLocation);
    obj.insert("remoteCityIndex", runShopBean->remoteCityIndex);
    obj.insert("remoteBuyGoods", runShopBean->remoteBuyGoods);
    obj.insert("time", runShopBean->time);
}



QByteArray RunShopBean::ArraytoJson(QList<RunShopBean*> *roleBeans)
{
    QJsonDocument doc;
    QJsonArray array;
    if(roleBeans->isEmpty()) return "";
    for(int i = 0; i< roleBeans->size(); i++){
        QJsonObject obj;
        RunShopBean *runshopBean = roleBeans->at(i);
        createObject(obj, runshopBean);
        array.append(obj);
    }
    doc.setArray(array);
    return doc.toJson();
}

void RunShopBean::setLine()
{
    CityNode *groupCityNode = CityManager::getInstance()->getCityNodeByName(groupCity);
    if(groupCityNode == nullptr){
        return;
    }
    groupCityEn = groupCityNode->enName;

    CityNode *remoteCityNode = CityManager::getInstance()->getCityNodeByName(remoteCity);
    if(remoteCityNode == nullptr){
        return;
    }
    remoteCityEn = remoteCityNode->enName;

    if(!CityManager::getInstance()->getRunShop(groupCityEn, remoteCityEn, &leaveLine, &backLine)){
        return;
    }
    CityNode *bangpai = new CityNode(groupCity, groupCityEn, 99, 156);//出城坐标
    leaveLine.insert(0, bangpai);
    CityNode * lastCityNode =backLine.last();
    CityXiaoWeiBean* xiaoWeiBean = CityManager::getInstance()-> getXiaoWeiBean(lastCityNode, groupXiaoWeiLocation);
    CityNode *xiaowei = new CityNode("校尉", XIAO_WEI, RunShopGoodsManager::getInstance()->getXiaoWeiLocationsImgs(groupXiaoWeiLocation), xiaoWeiBean->x , xiaoWeiBean->y);
    backLine.append(xiaowei);
    setRunshopType();
}

void RunShopBean::setRunshopType()
{

    groupBuyShopImg = ":/runshop/icon/runshop/runshop_liang.png";
    groupSellShopImg = ":/runshop/icon/runshop/runshop_liang2.png";
    groupStartScalar = Scalar(0, 61, 90);
    groupEndScalar=Scalar(11, 161, 251);
    groupBuyGoodsPrices = "94 50";
    groupSellGoodsPrices ="2 29 63";
    //    int runshopType; //跑商类型 0帮派之间 1帮派与洛阳或大理或苏州
    if (!remoteXiaoWeiLocation.isNull() && !remoteXiaoWeiLocation.isEmpty() && remoteXiaoWeiLocation != "无"){
        remoteLocationX = 147;
        remoteLocationY = 56;
        CityNode * lastCityNode =leaveLine.last();
        CityXiaoWeiBean *xiaoWeiBean = CityManager::getInstance()-> getXiaoWeiBean(lastCityNode, remoteXiaoWeiLocation);
        if(xiaoWeiBean == nullptr) return;
        leaveLine.append(new CityNode("校尉", XIAO_WEI,RunShopGoodsManager::getInstance()->getXiaoWeiLocationsImgs(remoteXiaoWeiLocation), xiaoWeiBean->x, xiaoWeiBean->y));
        backLine.insert(0,new CityNode(remoteCity,remoteCityEn,99, 156));
        runshopType = GROUP2GROUP;
        return;
    }else if(remoteCityEn == "dali"){
        remoteLocationX = 88;
        remoteLocationY = 161;
        runshopType = GROUP2DALI;
        remoteBuyShopImg = ":/runshop/icon/runshop/runshop_dalishi.png";
        remoteSellShopImg = ":/runshop/icon/runshop/runshop_dalishi2.png";
        remoteStartScalar= Scalar(100,3,165);
        remoteEndScalar = Scalar(113,42,236);
        return;
    }else if(remoteCityEn == "luoyang"){
        remoteLocationX = 177;
        remoteLocationY = 115;
        runshopType = GROUP2LUOYANG;
        remoteBuyShopImg = ":/runshop/icon/runshop/runshop_mudan.png";
        remoteSellShopImg = "qrc:/runshop/icon/runshop/runshop_mudan2.png";

        remoteStartScalar= Scalar(162, 95, 133);
        remoteEndScalar = Scalar(180, 189, 255);

        return;
    }else if(remoteCityEn == "suzhou"){
        remoteLocationX = 192;
        remoteLocationY = 126;
        runshopType = GROUP2SUZHOU;
        remoteBuyShopImg = ":/runshop/icon/runshop/runshop_tongjing.png";
        remoteSellShopImg = ":/runshop/icon/runshop/runshop_tongjing2.png";
        remoteStartScalar= Scalar(11, 141, 159);
        remoteEndScalar = Scalar(17, 170, 255);
        return;
    }
    runshopType = GROUP2ERROR;
}

